<template>
  <div class="app-container">

    <el-form :model="form" ref="form" label-width="100px" v-loading="formLoading" :rules="rules">
      <el-form-item label="用户名：" prop="userName" required>
        <el-input v-model="form.userName"></el-input>
      </el-form-item>
      <el-form-item label="密码：" prop="password" v-if="showPassword" required>
        <el-input v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item label="真实姓名：" prop="realName" required>
        <el-input v-model="form.realName"></el-input>
      </el-form-item>
      <el-form-item label="年龄：" prop="age">
        <el-input v-model="form.age"></el-input>
      </el-form-item>
       <el-form-item label="角色：" prop="roleIds" >
        <el-select v-model="form.roleIds" placeholder="角色" clearable>
           <el-option
                v-for="item in roleList"
                :key="item.roleId"
                :label="item.roleName"
                :value="item.roleId"
            ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="性别：" prop="sex">
        <el-select v-model="form.sex" placeholder="性别" clearable>
            <el-option
                v-for="item in this.sexList"
                :key="item.id"
                :label="item.sex"
                :value="item.id"
              ></el-option>
       
        </el-select>
      </el-form-item>
      <el-form-item label="出生日期：" prop="birthDay">
        <el-date-picker v-model="form.birthDay" type="date" placeholder="选择日期" />
      </el-form-item>
      <el-form-item label="手机：" prop="phone">
        <el-input v-model="form.phone"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
    
      
  </div>
</template>

<script>
import { mapGetters, mapState, mapActions } from "vuex";
import {selectUser,getOne,createUser, }from "@/api/system/user";
import Editor from '@/components/Editor';
export default {
  inject:['reload'],
   components: {
    Editor
  },
  data() {
    return {
      form: {
        id: null,
        userName: "",
        password: "",
        realName: "",
        roleIds: [],
        age: "",
        sex: "",
        birthDay: null,
        phone: null
      },
      sexList:[
          {sex: '男', id: 1},
          {sex: '女', id: 2}
      ],
      roleList:[],
      showPassword : true,
      formLoading: false,
      rules: {
        userName: [
          { required: true, message: "请输入用户名", trigger: "blur" }
        ],
        realName: [
          { required: true, message: "请输入真实姓名", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    //console.log(this.sexList)
    let id = this.$route.query.id;
    let _this = this;
    this.getRole();
    if (id && parseInt(id) !== 0) {
      _this.formLoading = true;
      getOne(id).then(re => {
        _this.form = re.data;
        _this.showPassword = false;
        // v-for="item in roleList"
        //         :key="item.roleId"
        //         :label="item.c"
        //         :value="item.roleId"
         re.roleIds.map(item => {
                {this.form.roleIds = item;}
              })
        //_this.form.roleIds= re.roleIds.item[0];
        console.log(this.form.roleIds)
        
        _this.formLoading = false;
      });
    }
  },
  methods: {
    formatSex: function(row, column) {
 
				return row.roleIds == [1] ? "超级管理员" : row.roleIds == 2 ? "普通角色": row.roleIds == 3 ? "小角色" : "暂无";
			},
     getRole() {

      this.formLoading=true;
      selectUser({ id: null})
        .then(res => {
          this.formLoading=false;
          if (res.code == 1) {
            this.roleList = res.roles;
          }
        })
        .catch(res => {
          this.formLoading=false;
        });
    },
      submitForm() {
      let _this = this;
      // this.roleIds.map(item => {
      //   form.roleIds.push(item);
      // });
      this.form.roleIds=[1];
      this.$refs.form.validate(valid => {
        if (valid) {
          this.formLoading = true;
          createUser(this.form)
            .then(data => {
              if (data.code === 1) {
                _this.$message.success('操作' + data.message);          
                _this.$router.push("/system/user/index");
              } else {
                _this.$message.error('操作' + data.message);
                _this.formLoading = false;
              }
            })
            .catch(e => {
              _this.formLoading = false;
            });
        } else {
          return false;
        }
      });
    },
    resetForm () {
      this.reload()
    }
  }
};
</script>